package net.lzzy.practices.web;

import com.alibaba.fastjson.JSONObject;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.FileUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.LoginInfo;
import net.lzzy.practices.web.models.RegisterInfo;
import net.lzzy.practices.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author hzl on 2022/5/12.
 * Description:
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_AUTH_ROOT)
public class AuthApiController {

    @Autowired
    public AuthApiController(UserService service) {
        this.service = service;
    }

    private final UserService service;

    @PostMapping(value = ApiConstant.ROUTE_AUTH_LOGIN, produces = ApiConstant.API_PRODUCES_JSON)
    public ApiResult login(@RequestBody LoginInfo json) {
        String username = json.getUsername();
        String password = json.getPassword();
        String salt = service.getSalt(username);
        User user = service.check(username, StringUtils.md5Encode(password, salt));

        if (user == null) {
            return new ApiResult(false, Messages.WRONG_PASSWORD.toString(), null);

        }
        if (user.getType() < 0) {
            return new ApiResult(false, Messages.USER_FORBIDDEN.toString(), null);
        }
        VmUser vmUser = VmUser.apiLogin(user);
        return new ApiResult(true, Messages.SUCCESS.toString(), vmUser);
    }

    @PostMapping(ApiConstant.ROUTE_AUTH_LOGOUT)
    public ApiResult logout(@RequestParam String token) {
        return new ApiResult(true, AuthUtils.logout(token), null);

    }

    @GetMapping(ApiConstant.ROUTE_AUTH_EXISTS)
    public ApiResult isUserExists(@PathVariable String user) {
        boolean exists = service.isUserNameOccupied(user);
        return new ApiResult(true, exists ? "用户名已经被使用" : "", exists);
    }

    @PostMapping(ApiConstant.ROUTE_AUTH_REGISTER)
    public ApiResult result(@RequestBody RegisterInfo info) {

        String nick = info.getNick();
        String user = info.getUser();
        String phone = info.getPhone();
        String avatar = info.getAvatar();
        String email = info.getEmail();
        String password = info.getPassword();
        boolean apply = info.isApply();
        String salt = UUID.randomUUID().toString();
        if (StringUtils.isEmpty(nick) ||
                StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(avatar) ||
                StringUtils.isEmpty(email) ||
                StringUtils.isEmpty(password)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        if (service.isUserNameOccupied(user)) {
            return new ApiResult(false, "用户名已经被使用", null);
        }
        User usr = new User();
        usr.setApplyTeacher(apply);
        usr.setType(User.TYPE_STUDENT);
        usr.setAvatar(avatar);
        usr.setEmail(email);
        usr.setPhone(phone);
        usr.setNickName(nick);
        usr.setPassword(StringUtils.md5Encode(password, salt));
        usr.setSalt(salt);
        usr.setUserName(user);
        VmUser vmUser = VmUser.apiLogin(service.register(usr));
        return new ApiResult(true, "注册成功", vmUser);
    }

    @Value("${user.pic}")
    private String imgPath;

    @PostMapping(ApiConstant.ROUTE_AUTH_UPLOAD)
    public ApiResult upload(@RequestParam MultipartFile file) {
        JSONObject result = FileUtils.upload(file, imgPath);
        return new ApiResult(result.getBoolean("status"),
                result.getString("message"),
                result.getString("url"));

    }
}
